﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RenLuyenModule
{
    public class ChuongTrinhDangThu
    {
        private Int32 MaDonVi;

        private RenLuyenModDataContext dbDangThu = new RenLuyenModDataContext();

        private IList<DOANSINH_SINHHOAT> sinhhoat = null;
        private IList<DOANSINH_SH_DANGTHU> dangthuDoanSinh = null;

        public ChuongTrinhDangThu()
        {
        }

        public ChuongTrinhDangThu(Int32 MaDonVi)
        {
            this.MaDonVi = MaDonVi;
        }

        #region Private Methods For Count And Percent Summary
        private string LayTenDonVi()
        {
            SOQUANLYDONVI donvi = dbDangThu.SOQUANLYDONVIs.SingleOrDefault(p => p.MASO == MaDonVi && p.HOATDONG == true);

            return donvi.TENDONVIDANGKY;

        }

        private Int32 LayNganh()
        {
            SOQUANLYDONVI donvi = dbDangThu.SOQUANLYDONVIs.SingleOrDefault(p => p.MASO == MaDonVi && p.HOATDONG == true);

            return donvi.NGANHID.Value;

        }
        private int countBoyScoutForUnit()
        {
            sinhhoat = dbDangThu.DOANSINH_SINHHOATs.Where(p => p.MADONVIID == MaDonVi && p.HOATDONG == true).ToList();
            return sinhhoat.Count;
        }

        private int countRegisterOnLine()
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi && p.HINHTHUCDANGKY==1).ToList();

            return dangthuDoanSinh.Count;
        }

        private int countRegisterOffline()
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi && p.HINHTHUCDANGKY == 0).ToList();

            return dangthuDoanSinh.Count;
        }

        private int unRegister()
        {

            int countRegister = countRegisterOnLine() + countRegisterOffline();
            int total = countBoyScoutForUnit();
            return total - countRegister;

        }
        private int countCerftificateLevel()
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi
                                                                 && p.NGAYCONGNHAN != null).ToList();

            return dangthuDoanSinh.Count;
        }

        private int countNonCerftificateLevel()
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi
                                                               && p.NGAYCONGNHAN == null).ToList();

            return dangthuDoanSinh.Count;
        }
        #endregion
        #region Private Methods For Count And Percent

        private int countRegisterOnLine(Int32 MaDangThuID)
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi && p.MADANGTHU == MaDangThuID && p.HINHTHUCDANGKY==1).ToList();

            return dangthuDoanSinh.Count;
        }

        private int countRegisterOffline(Int32 MaDangThuID)
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi && p.MADANGTHU == MaDangThuID && p.HINHTHUCDANGKY == 0).ToList();

            return dangthuDoanSinh.Count;
        }

        private int unRegister(Int32 MaDangThuID)
        {

            int countRegister = countRegisterOnLine(MaDangThuID) + countRegisterOffline(MaDangThuID);
            int total = countBoyScoutForUnit();
            return total-countRegister;

        }
        private int countCerftificateLevel(Int32 MaDangThuID)
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi
                                                                && p.MADANGTHU == MaDangThuID && p.NGAYCONGNHAN != null).ToList();

            return dangthuDoanSinh.Count;
        }

        private int countNonCerftificateLevel(Int32 MaDangThuID)
        {
            dangthuDoanSinh = dbDangThu.DOANSINH_SH_DANGTHUs.Where(p => p.MADONVIID == MaDonVi
                                                                && p.MADANGTHU == MaDangThuID && p.NGAYCONGNHAN == null).ToList();

            return dangthuDoanSinh.Count;
        }


        #endregion

        #region Public Methods For Bind Data

        public CustomModel.ThongKeSummary summaryReportLevelProgram()
        {
            CustomModel.ThongKeSummary item = new CustomModel.ThongKeSummary();
            item.MaDonVi = MaDonVi;
            item.TenDonVi = LayTenDonVi();
            item.SoDoanSinhTrongDonVi = countBoyScoutForUnit();
            item.SoDoanSinhDkyQuaTruong = countRegisterOffline();
            item.SoDoangSinhDKyTrucTuyen = countRegisterOnLine();
            item.SoDoanSinhChuaDangKy = unRegister();
            item.SoDoanSinhDaCongNhan = countCerftificateLevel();
            item.SoDoanSinhChuaCongNhan = countNonCerftificateLevel();

            decimal tyLeDangKyTrucTuyen = (countRegisterOnLine() / countBoyScoutForUnit()) * 100;
            decimal tyLeDangKyOffline = (countRegisterOffline() / countBoyScoutForUnit()) * 100;
            decimal tyLeCongNhan = (countCerftificateLevel() / countBoyScoutForUnit()) * 100;
            decimal tyLeChuaCongNhan = (countNonCerftificateLevel() / countBoyScoutForUnit()) * 100;

            item.TyLeDangKyTrucTuyen = tyLeDangKyTrucTuyen;
            item.TyLeDangKyQuaTruong = tyLeDangKyOffline;
            item.TyLeCongNhan = tyLeCongNhan;
            item.TyLeChuaCongNhan = tyLeChuaCongNhan;

            return item;
        }

        public List<CustomModel.ThongKeDangThu> listThongKe()
        {
            Int32 MaNganh = LayNganh();
            List<CustomModel.ThongKeDangThu> thongKeDangThu = new List<CustomModel.ThongKeDangThu>();
            IList<DANHMUCDANGTHU> danhMucDangThu = dbDangThu.DANHMUCDANGTHUs.Where(p => p.NGANHID == MaNganh).ToList();
            
            foreach(DANHMUCDANGTHU itemDangThu in danhMucDangThu)
            {
                Int32 MaDangThuID = itemDangThu.ID;
                string TenDangThu = itemDangThu.TENDANGTHU;

                CustomModel.ThongKeDangThu item = new CustomModel.ThongKeDangThu();

                item.MaDonVi = MaDonVi;
                item.TenDonVi = LayTenDonVi();
                item.SoDoanSinhTrongDonVi = countBoyScoutForUnit();
                item.SoDoanSinhDkyQuaTruong = countRegisterOffline(MaDangThuID);
                item.SoDoangSinhDKyTrucTuyen = countRegisterOnLine(MaDangThuID);
                item.SoDoanSinhChuaDangKy = unRegister(MaDangThuID);
                item.SoDoanSinhDaCongNhan = countCerftificateLevel(MaDangThuID);
                item.SoDoanSinhChuaCongNhan = countNonCerftificateLevel(MaDangThuID);
                item.MaDangThu = MaDangThuID;
                item.TenDangThu = TenDangThu;

                decimal tyLeDangKyTrucTuyen = (countRegisterOnLine(MaDangThuID) / countBoyScoutForUnit()) * 100;
                decimal tyLeDangKyOffline = (countRegisterOffline(MaDangThuID) / countBoyScoutForUnit()) * 100;
                decimal tyLeCongNhan = (countCerftificateLevel(MaDangThuID) / countBoyScoutForUnit()) * 100;
                decimal tyLeChuaCongNhan = (countNonCerftificateLevel(MaDangThuID) / countBoyScoutForUnit()) * 100;

                item.TyLeDangKyTrucTuyen = tyLeDangKyTrucTuyen;
                item.TyLeDangKyQuaTruong = tyLeDangKyOffline;
                item.TyLeCongNhan = tyLeCongNhan;
                item.TyLeChuaCongNhan = tyLeChuaCongNhan;


                thongKeDangThu.Add(item);
            }

            return thongKeDangThu;

        }


        #endregion

        #region Methods for services

        private List<CustomModel.TinhHinhDangThuModel> danhSachTinhHinhDangThu()
        {
            var qr = dbDangThu.TINHHINHDANGTHUDOANSINH(MaDonVi);
            if (qr == null)
            {
                return null;
            }
            else
            {
                List<CustomModel.TinhHinhDangThuModel> listDangThu = new List<CustomModel.TinhHinhDangThuModel>();
                foreach(var item in qr.ToList())
                {
                    CustomModel.TinhHinhDangThuModel items = new CustomModel.TinhHinhDangThuModel();
                    items.MaDonVi = item.MADONVIID;
                    items.TenDonVi = item.DONVIHIENTAI;
                    items.MaDonViCongNhan = (int)item.MADONVICONGNHAN;
                    items.TenDonViCongNhan = item.DONVICONGNHAN;
                    items.MaDoanSinh = item.MADOANSINH;
                    items.HoVaTen = item.HOLOT + " " + item.TEN;
                    items.NgaySinh = item.NGAYSINH.Value.ToShortDateString();
                    items.NoiSinh = item.NOISINH;
                    items.MaDangThu = item.MADANGTHU;
                    items.TenDangThu = item.TENDANGTHU;
                    items.NgayDangKy = item.NGAYDANGKYRENLUYEN;
                    items.HinhThucDangKy = item.PHUONGTHUCDANGKY;
                    items.NgayCongNhan = item.NGAYCONGNHAN;
                    items.TruongCongNhan = item.TENTRUONGCONGNHAN;
                    items.GioiTinh = item.PHAI;
                    items.PhuongThucDangKy = (int)item.HINHTHUCDANGKY;
                    items.DaCongNhan = (int)item.DACONGNHAN;
                    if (item.DACONGNHAN == 1)
                    {
                        items.TrangThai = "<img src='/Images/dacongnhan.png'/>";
                    }
                    else if (item.DACONGNHAN == 0)
                    {
                        items.TrangThai = "<img src='/Images/chuacongnhan.png'/>";
                    }
                    listDangThu.Add(items);
                }

                return listDangThu;
            }
        }

        public List<CustomModel.TinhHinhDangThuModel> layHetDanhSach()
        {
            return danhSachTinhHinhDangThu();
        }

        public List<CustomModel.TinhHinhDangThuModel> layDanhSachTimKiem(Int64 MaDoanSinh,string HoVaTen,Int32 MaDangThu,Int32 MaDonViCongNhan,string NgayCongNhan,string NgayDangKy,Int32 PhuongThucDangKy)
        {
            List<CustomModel.TinhHinhDangThuModel> lstTimKiem = danhSachTinhHinhDangThu();

            return lstTimKiem.Where(p => p.MaDoanSinh == MaDoanSinh ||
                                         p.HoVaTen.Contains(HoVaTen) ||
                                         p.MaDonViCongNhan == MaDonViCongNhan ||
                                         p.MaDangThu == MaDangThu ||
                                         p.NgayCongNhan == NgayCongNhan ||
                                         p.NgayDangKy == NgayDangKy ||
                                         p.PhuongThucDangKy == PhuongThucDangKy).ToList();
        }

        /// <summary>
        /// Lay danh sach ve lap danh sach tong hop tinh hinh dang thu cua don vi
        /// </summary>
        /// <param name="MaDonVi"></param>
        /// <returns></returns>
        public List<CustomModel.TongHopRenLuyen> layDanhSachTongHopDangThu(Int32 MaDonVi)
        {
            List<CustomModel.TongHopRenLuyen> lstTongHop = new List<CustomModel.TongHopRenLuyen>();
            var qu = dbDangThu.TONGHOP_RENLUYEN(MaDonVi);
            if (qu != null)
            {
                foreach (var item in qu.ToList())
                {
                    CustomModel.TongHopRenLuyen itemTongHop = new CustomModel.TongHopRenLuyen();

                    itemTongHop.MaDonVi = MaDonVi;
                    itemTongHop.TenDonVi = item.TENDONVISINHHOAT;
                    itemTongHop.HoVaTen = item.HOLOT + " " + item.TEN;
                    itemTongHop.NgaySinh = item.NGAYSINH.Value.ToShortDateString();
                    itemTongHop.NoiSinh = item.NOISINH;
                    itemTongHop.GioiTinh = item.PHAI;
                    itemTongHop.Email = item.EMAIL;
                    Nullable<Int32> inMaDangThu = item.MADANGTHUCAONHAT;
                    itemTongHop.MaDangThu = inMaDangThu.HasValue ? item.MADANGTHUCAONHAT.Value.ToString():"";
                    itemTongHop.TenDangThu = item.TENDANGTHUCAONHAT;
                    itemTongHop.SoDangThuThamGia = (int)item.SODANGTHU;

                    Nullable<DateTime> dtNgayCongNhan = item.NGAYCONGNHAN;

                    if (dtNgayCongNhan.HasValue)
                    {
                        itemTongHop.NgayCongNhan = dtNgayCongNhan.Value.ToShortDateString();
                    }
                    else
                    {
                        itemTongHop.NgayCongNhan = "";
                    }

                    itemTongHop.TruongCongNhan = item.TENTRUONGCONGNHAN;

                    lstTongHop.Add(itemTongHop);

                }

                return lstTongHop;
            }
            else
            {
                return null;
            }
        }
        #endregion

        
    }
}
